/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.*;
import java.util.Locale;

/**
 *
 * @author Kaoutar Azagagh
 */
public class ValutaController {
    
    private NumberFormat currencyFormatter;
    private DateFormat dateFormatter;
    private double amount;
    private Locale currentLocale;
    private String country;
    
    public void Convert(String price){
        
        //country = models.User.getCountry();
        country = "UK";
        currentLocale = new Locale(country);        
        amount = Double.parseDouble(price);
        currencyFormatter = NumberFormat.getCurrencyInstance(Locale.UK);
        dateFormatter = DateFormat.getDateInstance(DateFormat.LONG,
                                       currentLocale);
        
        //String am = price.replaceAll("[.]", "");
        //String amount = am.replaceAll(",", ".");
/*        
        //-- check for numbers and point, komma only
        if(!amount.matches("[0-9.,]+")){
            System.out.println("Please enter a valid amount");
        }
*/        
        String url = "http://finance.yahoo.com/q/bc?s=EURGBP=X&t=5d&l=on&z=m&q=l&c=";
        
        try {
            // Fetch results from Yahoo
            String results = "";
            URL resultPage = new URL(url);
            BufferedReader in = new BufferedReader(new InputStreamReader(resultPage.openStream()));

            String inputLine;

            while ((inputLine = in.readLine()) != null) {
              // Process each line.
              results = results + inputLine;
            }
            
            in.close();
            
            // Parrent matching
            Pattern finalPattern = Pattern.compile("x\">([0-9.,]+)</span>");
            Matcher matchFind = finalPattern.matcher(results);
            matchFind.find();
            
            double curRate = Double.valueOf(matchFind.group(1)).doubleValue();
            double totalConvert = curRate * Double.valueOf(amount).doubleValue();

            //-- shows amount in GBP
            System.out.println(String.format("Valuta in GBP: " + "%,.2f", totalConvert));
            
            System.out.println(currencyFormatter.format(totalConvert));
            
            
       } catch (Exception e) {
            //-- shows error message when try contains errors
            System.out.println( e.getMessage());
       }
    }
/*    
    public void setAmount(String s){
        try{
            
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
        
    }

    public String getAmount(){
        return currencyFormatter.format(amount);
    }
*/  
   
    public static void main (String[] args){
        ValutaController c = new ValutaController();
        c.Convert("1239.20");
    }

}
